import { Module } from 'vuex'

import {
  getCategoryGoodsCount,
  getCategoryGoodsSale,
  getCategoryGoodsFavor,
  getaddressGoodsSale,
  getGoodsSaleTop10,
  getGoodsAmountList
} from '@/service/main/analysis/dashboard'

import { IRootState } from '@/store/types'
import { IDashboard } from './types'

const dashboardModule: Module<IDashboard, IRootState> = {
  namespaced: true,
  state() {
    return {
      categoryGoodsCount: [],
      categoryGoodsSale: [],
      categoryGoodsFavor: [],
      addressGoodsSale: [],
      goodsSaleTop10: [],
      goodsAmountList: []
    }
  },
  mutations: {
    saveGoodsCount(state, countList) {
      state.categoryGoodsCount = countList
    },
    saveGoodsSale(state, saleList) {
      state.categoryGoodsSale = saleList
    },
    saveGoodsFavor(state, favorList) {
      state.categoryGoodsFavor = favorList
    },
    saveAddressSale(state, addressSaleList) {
      state.addressGoodsSale = addressSaleList
    },
    saveGoodsSaleTop10(state, goodsSaleTop10List) {
      state.goodsSaleTop10 = goodsSaleTop10List
    },
    saveGoodsAmountList(state, goodsAmountList) {
      state.goodsAmountList = goodsAmountList
    }
  },
  actions: {
    async getDashboardDataAction({ commit }) {
      const goodsCountResult = await getCategoryGoodsCount()
      commit('saveGoodsCount', goodsCountResult.data)
      const goodsSaleResult = await getCategoryGoodsSale()
      commit('saveGoodsSale', goodsSaleResult.data)
      const goodsFavorResult = await getCategoryGoodsFavor()
      commit('saveGoodsFavor', goodsFavorResult.data)
      const addressSaleResult = await getaddressGoodsSale()
      commit('saveAddressSale', addressSaleResult.data)
      const goodsSaleTop10Result = await getGoodsSaleTop10()
      commit('saveGoodsSaleTop10', goodsSaleTop10Result.data)
      const goodsAmountListResult = await getGoodsAmountList()
      commit('saveGoodsAmountList', goodsAmountListResult.data)
    }
  }
}

export default dashboardModule
